/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-23
 * @time 下午1:31:05
 */
package com.ssh.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.VcarTable;
import com.ssh.entity.Vehicle;
import com.ssh.model.pdata.VVisitorMPData;

/**
 * @author jack
 *
 */
public class VcarTableDAOImpl implements VcarTableDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#addVcarTable(com.ssh.entity.VcarTable)
	 */
	@Override
	public VcarTable addVcarTable(VcarTable vcarTable) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(vcarTable);
		} catch (Exception e) {
			return null;
		}
		return vcarTable;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#addVcarTable(com.ssh.entity.VcarTable[])
	 */
	@Override
	public int addVcarTable(VcarTable... vcarTables) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#deleteVcarTable(java.lang.String[])
	 */
	@Override
	public int deleteVcarTable(String... strings) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VcarTableDAOI#deleteVcarTableByVcarTableId(java.lang.String)
	 */
	@Override
	public VcarTable deleteVcarTableByVcarTableId(String vcarTableId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryVcarTable(java.lang.String[])
	 */
	@Override
	public List<VcarTable> queryVcarTable(String... strings) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VcarTableDAOI#queryVcarTableByVcarTableId(java.lang.String)
	 */
	@Override
	public VcarTable queryVcarTableByVcarTableId(String vcarTableId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#updateVcarTable(com.ssh.entity.VcarTable)
	 */
	@Override
	public VcarTable updateVcarTable(VcarTable vcarTable) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(vcarTable);
			return vcarTable;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VcarTableDAOI#updateVcarTable(com.ssh.entity.VcarTable[])
	 */
	@Override
	public int updateVcarTable(VcarTable... vcarTables) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryAllVVisitor()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitorMPData> queryAllVVisitor() {
		// TODO Auto-generated method stub
		String hql="";
		List<VVisitorMPData> vVisitorMPDataList = null;
		try {
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi) " +
					"from VcarTable as vt,Vehicle as v , Visitor as vi " +
					"where vt.visitorId=vi.visitorId and vt.vehicle.vehicleId=v.vehicleId";
			vVisitorMPDataList = sessionFactory.getCurrentSession().createQuery(hql).list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return vVisitorMPDataList ;
	}

	@SuppressWarnings("unchecked")
	@Override
	public VVisitorMPData queryOneVVisitor(String vehicleId,
			String visitorId,Timestamp inTime) {
		// TODO Auto-generated method stub
		String hql="";
		VVisitorMPData vVisitorMPData = null;
		try {
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi)  " +
					"from VcarTable vt , Vehicle as v , Visitor as vi  " +
					"where (vt.visitorId=vi.visitorId and vt.vehicle.vehicleId=v.vehicleId) " +
					"and (vt.vcarInTime=:inTime and vt.visitorId=:visitorId and v.vehicleId=:vehicleId) ";
			Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter("inTime", inTime)
					.setParameter("visitorId", visitorId).setParameter("vehicleId", vehicleId);
			Hibernate.initialize(Vehicle.class);
			vVisitorMPData = (VVisitorMPData)query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return vVisitorMPData ;
	}

	@SuppressWarnings("unchecked")
	@Override
	public VVisitorMPData queryOneVVisitor(String vehicleId,Timestamp inTime) {
		// TODO Auto-generated method stub
		String hql="";
		VVisitorMPData vVisitorMPData = null;
		try {
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi)  " +
					"from VcarTable vt , Vehicle as v , Visitor as vi  " +
					"where (vt.visitorId=vi.visitorId and vt.vehicle.vehicleId=v.vehicleId) " +
					"and (vt.vcarInTime=:inTime and v.vehicleId=:vehicleId) ";
			Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter("inTime", inTime)
					.setParameter("vehicleId", vehicleId);
			Hibernate.initialize(Vehicle.class);
			vVisitorMPData = (VVisitorMPData)query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return vVisitorMPData ;
	}
	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryVVisitorBV(com.ssh.entity.VcarTable)
	 */
	@Override
	public List<VVisitorMPData> queryVVisitorBV(VcarTable vcarTable) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryVVisitorBV(com.ssh.entity.Vehicle)
	 */
	@Override
	public List<VVisitorMPData> queryVVisitorBV(Vehicle vehicle) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryVVisitorBV(java.lang.String)
	 */
	@Override
	public List<VVisitorMPData> queryVVisitorBV(String vehicleId) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryBVV(com.ssh.entity.VcarTable,
	 * com.ssh.entity.Vehicle)
	 */
	@Override
	public List<VVisitorMPData> queryBVV(VcarTable vcarTable, Vehicle vehicle) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VcarTableDAOI#queryBVV(com.ssh.entity.Vehicle,
	 * com.ssh.entity.VcarTable)
	 */
	@Override
	public List<VVisitorMPData> queryBVV(Vehicle vehicle, VcarTable vcarTable) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public VVisitorMPData queryAllLVVInfo(String vehicleId, String visitorId,
			String state) {
		// TODO Auto-generated method stub
		String hql = "select new com.ssh.model.pdata.VVisitorMPData(vt) "
				+ " from VcarTable as vt ,Vehicle v,Visitor vs where vt.visitorId=:visitorId and "
				+ "vt.vcarState=:vcarState and vt.vehicle.vehicleId=:vehicleId and vt.visitorId=vs.visitorId"
				+ " vt.vehicle.vehicleId=v.vehicleId";
		Query query = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("visitorId", visitorId)
				.setParameter("vehicleId", vehicleId)
				.setParameter("vcarState", state);
		return (VVisitorMPData) query.uniqueResult();
	}

	@Override
	public VVisitorMPData queryAllLVVInfo(String uniqueId,
			String state) {
		// TODO Auto-generated method stub
		VVisitorMPData locVVisitorMPData ;
		try{
			String hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vs) "
					+ " from VcarTable as vt ,Vehicle v,Visitor vs where " +
					" (vt.vehicle.vehicleId=v.vehicleId  and vt.visitorId=vs.visitorId and vt.vcarState=:vcarState) " +
					" and (vt.visitorId=:uniqueId  or vt.vehicle.vehicleId=:uniqueId)";
			locVVisitorMPData = (VVisitorMPData) sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("uniqueId", uniqueId)
					.setParameter("vcarState", state).uniqueResult();
		}
        catch(Exception e){
        	e.printStackTrace();
        	return null;
        }
		return locVVisitorMPData;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitorMPData> queryVVisitorByVehicleId(String vehicleId) {
		// TODO Auto-generated method stub
		String hql="";
		List<VVisitorMPData> vVisitorMPDataList = null;
		try {
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi) " +
					"from VcarTable as vt,Vehicle as v , Visitor as vi " +
					"where vt.visitorId=vi.visitorId and vt.vehicle.vehicleId=v.vehicleId and vt.vehicle.vehicleId=:vehicleId ";
			vVisitorMPDataList = sessionFactory.getCurrentSession().createQuery(hql).setParameter("vehicleId", vehicleId).list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return vVisitorMPDataList ;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitorMPData> queryVVisitorByInTime(Timestamp inTime) {
		// TODO Auto-generated method stub
		String hql="";
		long endTime = inTime.getTime()+30*60*1000;
		long startTime = inTime.getTime()-30*60*1000;
		Timestamp endTimes=new Timestamp(0),startTimes = new Timestamp(0);
		startTimes.setTime(startTime);
		endTimes.setTime(endTime);
		List<VVisitorMPData> vVisitorMPDataList = null;
		try {
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi) " +
					"from VcarTable as vt,Vehicle as v , Visitor as vi " +
					"where (vt.visitorId=vi.visitorId and vt.vehicle.vehicleId=v.vehicleId) " +
					" and vt.vcarInTime between :startTimes and :endTimes";
			vVisitorMPDataList = sessionFactory.getCurrentSession().createQuery(hql).
					setParameter("startTimes", startTimes).setParameter("endTimes", endTimes).list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return vVisitorMPDataList ;
	}

	@Override
	public VVisitorMPData queryByVehicleId(String vehicleId, String state) {
		// TODO Auto-generated method stub
		VVisitorMPData vVisitorMPData ;
		String hql;
		try{
			hql = "select new com.ssh.model.pdata.VVisitorMPData(vt,v,vi) " +
					" from VcarTable as vt,Vehicle as v , Visitor as vi " +
					" where vt.vehicle.vehicleId=v.vehicleId and vt.vehicle.vehicleId=:vehicleId " +
					" and vt.vcarState=:vcarState ";
			vVisitorMPData = (VVisitorMPData) sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("vehicleId", vehicleId)
					.setParameter("vcarState", state).uniqueResult();
			return vVisitorMPData;
		}
        catch(Exception e){
        	e.printStackTrace();
        	return null;
        }
	}

}
